import { message } from 'ant-design-vue'

/**
 *
 * @param type 提示的类型 success/error
 * @param msg 成功的提示信息
 * @param errMsg 失败的提示信息
 */
export const toast = (
    type: string = 'success',
    msg: string = '操作成功',
    errMsg: string = '操作失败'
) => {
    if (type === 'success') {
        message.success(msg)
    } else {
        message.error(errMsg)
    }
}

/**
 * 固定列表宽度
 * @param wd1 100px
 * @param wd2 120px
 */
export const COLUMNS_WIDTH = {
    id: '50px',
    wd1: '100px',
    wd2: '120px',
    wd3: '150px',
    wdDate: '200px',
    wdBtn: '160px'
}

/**
 *用于从sider数组中获取当前路由的索引
 * @param arr slider数组路由
 * @param path  当前路由
 * @returns [ ] 返回一个数组
 */
export function getSelectFromSliders(arr: any[], path: string) {
    if (arr.length === 0 || Array.isArray(arr) === false) {
        return []
    }
    let result: any = null
    arr.forEach((item: any) => {
        if (result) return // 如果找到了，就不再继续遍历，防止找到之后，出现重复赋值，导致最后值为空的问题
        if (item.path === path) {
            result = item
        }
        if (item.children && item.children.length > 0) {
            const res: any = getSelectFromSliders(item.children, path)
            result = res
        }
    })
    return result
}

import useMainStore from '@/stores'
/**
 * @param 适用情况，传如router对象，path，query，跳转路由，统一修改侧边栏选中状态的选中状态
 * @param router 路由对象
 * @param path 路由地址
 * @param query 路由参数（可选）
 * @param isPush 是否跳转路由 默认跳转
 * @returns  不返回数据
 *
 */
export const routerPush = (router: any, path: string, query?: any, isPush: boolean = true) => {
    const LayoutStore = useMainStore().useLayoutStore()
    LayoutStore.nowSliderKeys = [path]

    if (isPush) {
        if (query) {
            router.push({
                path,
                query
            })
        } else {
            router.push(path)
        }
    }
}

import { sliders } from '@/router/constantRouter'
/**
 * @param 适用情况，非侧边栏选中时，更新navRouterList
 *@params type 为 push 或 splice
 *@params keys 为数组  内容为路由路径
 *@params 解决：在非侧边栏选中时，navRouterList 中的数据没有及时更新
 */

export const EditNavRouterList = (type: string, keys: string[]) => {
    const LayoutStore = useMainStore().useLayoutStore()
    const { navRouterList } = LayoutStore
    const index = LayoutStore.navRouterList.findIndex((el) => el.key === keys[0])
    const res = getSelectFromSliders(sliders, keys[0])
    if (type === 'push' && index <= -1) {
        navRouterList.push(res)
    } else if (type === 'delete') {
        navRouterList.splice(index, 1)
    }
}

/**从options里获取map数据，用于映射
 * @params options 选项数组
 */
export const getMapFromOption = (options: any[]) => {
    let map = {} as any
    options.forEach((el) => {
        map[el.value] = el.label
    })
    return map
}

/**
 *将json数组转化为对象
 * @param json js字符串
 * @returns
 */

export const getObjectFromJSON = (json: string) => {
    if (!json) return []
    let obj = {} as any
    for (const { label, value, ...keys } of JSON.parse(json)) {
        let key = Object.keys(keys)[0] || ''
        obj[key] = value
    }
    return obj
}
